package de.geckocraft.iConomyExchange.main;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import de.geckocraft.iConomyExchange.mysql.broker;
import de.geckocraft.iConomyExchange.mysql.mysqlconfig;


public class configbymysql
{
	private static ArrayList<ExServer>	ExServers = new ArrayList<ExServer>();
	
	public static void loadConfig()
	{
		createDefaultConfig();
		loadExServer();
	}
	
	public static void createDefaultConfig()
	{
		
		if(!broker.tableexist("ExchangeConfig"))
		{
			broker.sendUpdate(
			"CREATE TABLE ExchangeConfig ("
			+ "id INTEGER NOT NULL AUTO_INCREMENT, "
			+ "Servername VARCHAR(255) NOT NULL, "
			+ "balancetabellenname VARCHAR(255) NOT NULL, "
			+ "accounttabellenname VARCHAR(255) NOT NULL, "
			+ "serverip VARCHAR(255) NOT NULL, "
			+ "serverport VARCHAR(255) NOT NULL, "
			+ "serverdatabase VARCHAR(255) NOT NULL, "
			+ "serveruser VARCHAR(255) NOT NULL, "
			+ "serverpasswort VARCHAR(255), "
			+ "serverwaehrung VARCHAR(255) NOT NULL,"
			+ "PRIMARY KEY ( id ))");
			
			broker.sendUpdate("INSERT INTO ExchangeConfig (Servername,balancetabellenname,accounttabellenname,serverip,serverport,serverdatabase,serveruser,serverpasswort,serverwaehrung) "
					+ "values ('Servername','cc3balance', 'cc3account', ' "
					+ mysqlconfig.getconfig().getString("ip")
					+ "', '" + mysqlconfig.getconfig().getString("port") 
					+ "', '" + mysqlconfig.getconfig().getString("database") 
					+ "', '" + mysqlconfig.getconfig().getString("user")
					+ "', '" + mysqlconfig.getconfig().getString("password") 
					+ "', 'Doller')");
		}
		
		if(!broker.tableexist("ExchangeServerToServer"))
		{
			broker.sendUpdate(
					"CREATE TABLE ExchangeServerToServer ("
					+ "id INTEGER NOT NULL AUTO_INCREMENT, "
					+ "ServeridVon INTEGER NOT NULL,"
					+ "ServeridZu INTEGER NOT NULL,"
					+ "Wechselkurs DOUBLE NOT NULL,"
					+ "PRIMARY KEY ( id ))");
		}
		 
		ArrayList<String>	serverId1 = new ArrayList<String>(); 
		ResultSet rs1 = broker.sendStatement("select id from ExchangeConfig");
		try
		{
			while (rs1.next()) 
			{
				serverId1.add(rs1.getString(1));
			}
		} catch (SQLException e1)
		{
			e1.printStackTrace();
		}
		Iterator<String> i1 = serverId1.iterator();

		
		try
		{
			while (i1.hasNext()) 
			{
				String ServeridVon = i1.next();
				
				ArrayList<String>	serverId2 = new ArrayList<String>(); 
				ResultSet rs2 = broker.sendStatement("select id from ExchangeConfig");
				try
				{
					while (rs2.next()) 
					{
						serverId2.add(rs2.getString(1));
					}
				} catch (SQLException e2)
				{
					e2.printStackTrace();
				}
				Iterator<String> i2 = serverId2.iterator();
				
				
				while (i2.hasNext()) 
				{
					boolean exist = false;
					
					String ServeridZu = i2.next();
					
					if(!ServeridVon.equals(ServeridZu))
					{
						ResultSet rs3 = broker.sendStatement("select * from ExchangeServerToServer where ServeridVon = " + ServeridVon + " and ServeridZu = " + ServeridZu);
						while (rs3.next()) 
						{
						exist = true;
						}
						if(!exist)
						{
							System.out.println(ServeridVon);
							System.out.println(ServeridZu);
							broker.sendUpdate("INSERT INTO ExchangeServerToServer (ServeridVon,ServeridZu,Wechselkurs) values('" + ServeridVon + "', '" + ServeridZu +"',0.01)");
						}
					}
				}
			}
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	public static void loadExServer()
	{
		ArrayList<String>	alId = new ArrayList<String>();
		ArrayList<String>	alName = new ArrayList<String>(); 
		ArrayList<String>	alBalancetabellen  = new ArrayList<String>();
		ArrayList<String>	alAccounttabellen  = new ArrayList<String>();
		ArrayList<String>	alIp  = new ArrayList<String>();	
		ArrayList<String>	alPort  = new ArrayList<String>();
		ArrayList<String>	alDatabase  = new ArrayList<String>();
		ArrayList<String>	alUser  = new ArrayList<String>();
		ArrayList<String>	alPasswort  = new ArrayList<String>();
		ArrayList<String>	alWaehrung  = new ArrayList<String>();
		
		
		ResultSet rs1 = broker.sendStatement("select * from ExchangeConfig");
		try
		{
			while (rs1.next()) 
			{
				alId.add(rs1.getString(1));
				alName.add(rs1.getString(2));
				alBalancetabellen.add(rs1.getString(3));
				alAccounttabellen.add(rs1.getString(4));
				alIp.add(rs1.getString(5));
				alPort.add(rs1.getString(6));
				alDatabase.add(rs1.getString(7));
				alUser.add(rs1.getString(8));
				alPasswort.add(rs1.getString(9));
				alWaehrung.add(rs1.getString(10));
				
			}
		} catch (SQLException e1)
		{
			e1.printStackTrace();
		}
		
		
		
		Iterator<String> iId= alId.iterator();
		Iterator<String> iName= alName.iterator();
		Iterator<String> iBalancetabellenname= alBalancetabellen.iterator();
		Iterator<String> iAccounttabellenname= alAccounttabellen.iterator();
		Iterator<String> iIp= alIp.iterator();
		Iterator<String> iPort= alPort.iterator();
		Iterator<String> iDatabase= alDatabase.iterator();
		Iterator<String> iUser= alUser.iterator();
		Iterator<String> iPasswort= alPasswort.iterator();
		Iterator<String> iWaehrung= alWaehrung.iterator();
		
		
		while(iId.hasNext())
		{
			String serverID = iId.next();

			String servername = iName.next();
			String serverBalancetabelle = iBalancetabellenname.next();
			String serverAccounttabelle = iAccounttabellenname.next();
			
			String databaspfad = "jdbc:mysql://"+ iIp.next().trim()+ ":" + iPort.next()+ "/" + iDatabase.next(); 
			String databaseuser = iUser.next();
			String databasepw = iPasswort.next();
			String waehrung = iWaehrung.next();
			
			
			HashMap< String, Double> wechselkurs = new HashMap<String, Double>();
			
			ResultSet rs2 = broker.sendStatement("select ExchangeConfig.Servername,ExchangeServerToServer.Wechselkurs from ExchangeServerToServer,ExchangeConfig where ExchangeServerToServer.ServeridVon = " + serverID + " and ExchangeServerToServer.ServeridZu = ExchangeConfig.id ");
			
			try
			{
				while (rs2.next()) 
				{
					wechselkurs.put(rs2.getString(1).toLowerCase(), rs2.getDouble(2));
				}
			
					
			}catch (SQLException e1)
			{
				e1.printStackTrace();
			}
			
			
			ExServer exchanges = new ExServer(servername, serverBalancetabelle,serverAccounttabelle, wechselkurs, databaspfad,databaseuser,databasepw,waehrung);
			
			if(exchanges != null){
				ExServers.add(exchanges);
			}
		}
	}

	public static ArrayList<ExServer> getExServers()
	{
		return ExServers;
	}
}
